package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.List;
import java.util.Set;

/**
 * @description:
 * @author: 韩汶锦
 * @date: 2025/4/3
 */
@Service
public class SetmealServiceImpl implements SetmealService {
    private static final Logger log = LoggerFactory.getLogger(SetmealServiceImpl.class);
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private DishService dishService;

    /**
     * @description: 分页查询
     * @author: 韩汶锦
     * @date: 2025年04月03日
     *  * @Param setmealPageQueryDTO
     *  * @Return com.sky.result.PageResult
     **/
    @Override
    public PageResult selectByPage(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());

        Page<SetmealVO> page = setmealMapper.selectByPage(setmealPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * @description: 新增套餐
     * @author: 韩汶锦
     * @date: 2025年04月03日
     *  * @Param setmealDTO
     *  * @Return void
     **/
    @Override
    @Transactional
    public void save(SetmealDTO setmealDTO) {
        Setmeal setmeal =new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.insert(setmeal);
        Long id  = setmeal.getId();
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(id);
        }
        setmealMapper.insertSermealDish(setmealDishes);
    }

    /**
     * @description: 根据id查询套餐
     * @author: 韩汶锦
     * @date: 2025年04月03日
     *  * @Param id
     *  * @Return com.sky.vo.SetmealVO
     **/
    @Override
    public SetmealVO selectById(Long id) {
        SetmealVO setmealVO = setmealMapper.selectById(id);
        return setmealVO;
    }

    /**
     * @description: 修改套餐
     * @author: 韩汶锦
     * @date: 2025年04月03日
     *  * @Param setmealDTO
     *  * @Return void
     **/
    @Override
    public void update(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.update(setmeal);
        setmealMapper.deleteSetmealDishById(setmealDTO.getId());
        for (SetmealDish setmealDish : setmealDTO.getSetmealDishes()) {
            setmealDish.setSetmealId(setmealDTO.getId());
        }
        setmealMapper.insertSetmealDish(setmealDTO.getSetmealDishes());
    }

    /**
     * @description: 修改套餐状态
     * @author: 韩汶锦
     * @date: 2025年04月03日
     *  * @Param id
     * @Param status
     *  * @Return void
     **/
    @Override
    public void updateStatus(Long id, String status) {
        setmealMapper.updateStatus(id,status);
    }

    /**
     * @description: 批量删除套餐
     * @author: 韩汶锦
     * @date: 2025年04月03日
     *  * @Param ids
     *  * @Return void
     **/
    @Transactional
    @Override
    public void delete(String ids) {
        String[] id = ids.split(",");
        for (String s : id) {
            Long i = Long.parseLong(s);
            setmealMapper.deleteById(i);
            setmealMapper.deleteSetmealDishById(i);
        }
    }


    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }

}
